
function toggleButton(cup) {
    this.coef = 1;
    // 选择回调函数
    this.checkedCallBack = null;
    // 取消回调函数
    this.noCheckedCallBack = null;

    this.cup = cup;
    if(!this.cup) return false;

    // 结构初始化
    this.__initStruct();

    // 框架
    this.ToggleButton = this.cup.querySelector(".ToggleButton");
    // 按钮
    this.checkbox = this.cup.querySelector("input");
}

// 类型遍历
toggleButton.prototype.__toggleButtonName = 0;

// 结构初始化
toggleButton.prototype.__initStruct = function() {
    let templet = `
        <!-- 按钮组件 -->
        <div class="ToggleButton">
            <label for="toggleButton_${toggleButton.prototype.__toggleButtonName}">
                <input type="checkbox" id="toggleButton_${toggleButton.prototype.__toggleButtonName++}">
                <span></span>
            </label>
        </div>
    `;

    this.cup.innerHTML = templet;
}

// 返回 checkBox
toggleButton.prototype.getCheckBox = function() {
    return this.checkbox;
}

// 设置选中事件
toggleButton.prototype.setCheckedCallBack = function(callBack) {
    if(typeof callBack !== "function")
        return false;
    this.checkedCallBack = callBack;
}

// 执行选中事件
toggleButton.prototype.__runCheckedCallBack = function() {
    if(typeof this.checkedCallBack !== "function")
        return false;
    this.checkedCallBack();
}

// 设置取消事件
toggleButton.prototype.setNoCheckedCallBack = function(callBack) {
    if(typeof callBack !== "function")
        return false;
    this.noCheckedCallBack = callBack;
}

// 执行取消事件
toggleButton.prototype.__runNoCheckedCallBack = function() {
    if(typeof this.noCheckedCallBack !== "function")
        return false;
    this.noCheckedCallBack();
}

// 启动监听
toggleButton.prototype.addClickRespon = function() {
    this.checkbox.addEventListener("click", () => {
        if(this.checkbox.checked)
            this.__runCheckedCallBack();
        else
            this.__runNoCheckedCallBack();
    });
}

// 大小设置
toggleButton.prototype.setSize = function(coef) {
    this.coef = coef || this.coef;
    this.ToggleButton.style.setProperty("--controlLen", String(window.innerWidth * this.coef));
}

function createToggleButton(cup) {
    return new toggleButton(cup || null);
}

export var ToggleButton = createToggleButton;